﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Generic = System.Collections.Generic;

namespace iPdf
{
    class SplitPage : ActionPage<SplitItem, PdfLetOption>
    {
        #region Fields
        private Dictionary<string, int> showUpTimeDict;
        #endregion

        #region Properties
        public override PdfLetOption PdfLets
        {
            get
            {
                return new PdfLetOption(true, false, false);
            }
        }

        protected override PdfLetOption PageOption
        {
            get
            {
                return Global.MainWindow.PdfLets;
            }
        }

        protected override string DestinationSuffix
        {
            get
            {
                return ".Part";
            }
        }

        public override string IntroInfo
        {
            get
            {
                return global::iPdf.Properties.Resources.SplitPageIntroInfo;
            }
        }
        #endregion

        #region Ctor
        public SplitPage()
            : base()
        {
            showUpTimeDict = new Dictionary<string, int>();
            visibleColumns.Add(Global.Grid.Destination);
            visibleColumns.Add(Global.Grid.From);
            visibleColumns.Add(Global.Grid.To);

            actor = new PdfSplitter();
        }
        #endregion

        #region Methods

        protected override SplitItem GetIthActionItem(TataGrid grid, int i)
        {
            SplitItem item = new SplitItem();
            item.Source = (string)grid.Source.GetValueAt(i);
            item.Destination = (string)grid.Destination.GetValueAt(i);
            
            item.From = (int)grid.From.GetValueAt(i);
            item.To = (int)grid.To.GetValueAt(i);

            return item;
        }

        public override string GetDestination(string item, string outputFolder)
        {
             int showUpTime = 1;
             if (showUpTimeDict.ContainsKey(item))
             {
                 showUpTime = showUpTimeDict[item] + 1;
             }
            showUpTimeDict[item] = showUpTime;
            string destination = base.GetDestination(item, outputFolder);
            int lastDotIndex = destination.LastIndexOf(Helper.Dot);

            return destination.Insert(lastDotIndex,  showUpTime.ToString());
        }

        public override void OnTabPageChanged()
        {
            base.OnTabPageChanged();

            showUpTimeDict.Clear();
        }

        #endregion
    }
}
